/**
 * Created by Adhub on 2017/7/25.
 */
var STATIC_URL = {
    // 'domian':'http://hubdsp.hubcloud.com.cn',
    'domian':'http://ads.hubcloud.com.cn',
};

var setting = {
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};

//获取当前时间戳 reutrn YYYY-MM-DD
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    //var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }


    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;

    return currentdate;

};

/***参数都是以周一为基准的***/
//上周的开始时间 getTime(7,1)
//上周的结束时间 getTime(1,1)
//本周的开始时间 getTime(0,1)
//本周的结束时间 getTime(-6,1)
function getTime(n,m) {
    var now = new Date();
    var year = now.getFullYear();
    //因为月份是从0开始的,所以获取这个月的月份数要加1才行
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var day = now.getDay();
    // console.log(date);
    //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
    if (day !== 0) {
        n = n + (day - 1);
    } else {
        n = n + day;
    }
    if (day) {
        //这个判断是为了解决跨年的问题
        if (month > 1) {
            month = month;
        }
        //这个判断是为了解决跨年的问题,月份是从0开始的
        else {
            year = year - 1;
            month = 12;
        }
    }
    now.setDate(now.getDate() - n);
    year = now.getFullYear();
    month = now.getMonth() + 1;
    date = now.getDate();
    // console.log(n);
    if(m == 1) {
        s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date); //m=1,2018-01-01
    } else if(m == 2){
        s = year + "年" + (month < 10 ? ('0' + month) : month) + "月" + (date < 10 ? ('0' + date) : date) + '日'; //m=2,2018年01月01日
    }     
    return s;
};

//传入一个  YYYYY-MM-DD  前推和后退数以天数
function nextDayFnc(startTime,y,z){
    var startTime = startTime;
    var startTime2 = startTime.split("-");
    var year = parseInt(startTime2[0]);
    var month = parseInt(startTime2[1]);
    var day = parseInt(startTime2[2]);
    var myDate=new Date();
    var setYesDay =  parseInt(myDate.setFullYear(year,month-1,day));
    if(z==1){
        var yesterday = parseInt(setYesDay-86400000*y);     //加减天数改变这个毫秒数
    }else if(z==2){
        var yesterday = parseInt(setYesDay+86400000*y);      //z==1时为-  z==2时为+
    }
    var ds = new Date(yesterday);
    var oldYear=ds.getYear()+1900;
    var oldMonth = ds.getMonth()+1;
    if (oldMonth<10){
        oldMonth = "0"+oldMonth;
    }else{
        oldMonth = oldMonth;
    }
    var oldDay = ds.getDate();
    if(oldDay<10){
        oldDay = "0"+oldDay;
    }else{
        oldDay = oldDay;
    }
    var yesDay = oldYear+"-"+oldMonth+"-"+oldDay;
    return yesDay;
};
//获取上一个月的日期
function prevDate(date) {
    var months = [31,28,31,30,31,30,31,31,30,31,30,31];
    var dates = date.split("-");
    if (dates[0] % 4 == 0) {
        months[1] = 29;
    }
    var str = "";
    if (parseInt(dates[1]) != 1) {
        if (parseInt(dates[2]) > months[parseInt(dates[1]) - 2]) {
            str = dates[0] + "-" + (parseInt(dates[1]) > 9 ? "" + (parseInt(dates[1]) - 1) : "0" + ((parseInt(dates[1])) - 1)) + "-" + months[parseInt(dates[1]) - 2];
        } else {
            str = dates[0] + "-" + (parseInt(dates[1]) > 9 ? "" + (parseInt(dates[1]) - 1) : "0" + ((parseInt(dates[1])) - 1)) + "-" + (parseInt(dates[2]) > 9 ? "" + parseInt(dates[2]) : "0" + parseInt(dates[2]));
        }
    } else {
        if (parseInt(dates[2]) > months[11]) {
            str = (parseInt(dates[0]) - 1) + "-12" + "-" + months[11];
        } else {
            str = (parseInt(dates[0]) - 1) + "-12" + "-" + (parseInt(dates[2]) > 9 ? "" + parseInt(dates[2]) : "0" + parseInt(dates[2]));
        }
    }
    return str;
};
//获取当前的小时
function getNowHM(){
    var date  = new Date();
    var seperator = ":";
    var hours = date.getHours();
    var minut = date.getMinutes();
    if(hours>=0 && hours<=9 ){
        hours = "0"+hours;
    }
    if(minut>0 && minut<=9 ){
        minut = "0"+minut;
    }
    var nowHM = hours+seperator+minut;
    return nowHM;
};
//取到最近一个能被5整除的数
function fiveMultiple(val) {
    if (val <= 0) {return 0;}
    return val - val % 5 + 5
}
//传入 YYYYY-MM-DD 转成 yyyy年mm月dd日
function ymd(date){
    var flag = date.slice(4, 5);
    if(flag=="-"){
        var newDate = date.split("-");
        var ymdTime = newDate[0]+"年"+newDate[1]+"月"+newDate[2]+"日";
        return ymdTime;
    }

};
function getQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  decodeURI(r[2]); return null;
}
// 下载报表
function exportCsv (obj,name) {
    var titleForKey = obj.titleForKey;
    var data = obj.data;
    var str = [];
    str.push(obj.title.join(",")+"\r\n");
    for(var i=0;i<data.length;i++){
        var temp = [];
        for(var j=0;j<titleForKey.length;j++){
            temp.push(data[i][titleForKey[j]]);
        }
        str.push(temp.join(",")+"\r\n");
    }
    var uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str.join(""));

    var downloadLink = document.createElement("a");
    downloadLink.href = uri;
    downloadLink.download = name;
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink);
};
// 上下双滚动条
// function floatScroll($box) {
//     var mouseX,objX,
//         oldTime = new Date(),
//         content = $box.find(".slider-content")[0],
//         isDowm = false,
//         fW = $box[0].clientWidth,
//         sW = content.clientWidth,
//         ratio = fW / sW,
//         bW = fW * ratio,
//         max = fW - bW,
//         scroll = $box.find(".bar");
//     $box.addClass("slider-box")
//     scroll.width(bW);
//     $(document).off("mousedown").on("mousedown",".slider-bar > .bar",function(e){
//         objX = this.style.left || 0;
//         mouseX = e.clientX;
//         isDowm = true;
//     })
//     document.onmousemove = function (e) {
//         if (isDowm) {
//             var x = e.clientX;
//             var target = 0;
//             var left = parseInt(objX) + parseInt(x) - parseInt(mouseX);
//             if(left < 0 ) {
//                 target = 0;
//             } else if (left > max) {
//                 target = max;
//             } else {
//                 target = left;
//             }
//             scroll[0].style.left = target + "px";
//             content.style.marginLeft = -target/max*(sW-fW) + "px";
//         }
//     }
//     document.onmouseup = function (e) {
//         if (isDowm) {
//             isDowm = false;
//         }
//     }

//     document.onscroll = function() {
//         var nowTime = new Date();
//         var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
//         if(nowTime - oldTime > 100) {
//             oldTime = nowTime;
//             // 判断滚动条控件是否超过分页控件
//             if(scrollTop > $(".page").offset().top - $(window).height() ) {
//                 scroll.parent().removeClass("bottom")
//             } else {
//                 scroll.parent().addClass("bottom")
//             }
//         }
//     }
// }
function floatScroll($box,callback) {
    var mouseX,objX,
        oldTime = new Date(),
        content = $box.find(".slider-content")[0],
        isDowm = false,
        fW = $box[0].clientWidth,
        sW = content.clientWidth,
        ratio = fW / sW,
        bW = fW * ratio,
        max = fW - bW,
        $body = $("body")
        scroll = $box.find(".bar");
    // if(ratio == 1) {
    //     return;
    // }
    $box.addClass("slider-box")
    scroll.width(bW);
    $(document).off("mousedown").on("mousedown",".slider-bar > .bar",function(e){
        objX = this.style.left || 0;
        mouseX = e.clientX;
        isDowm = true;
        $body.addClass('select-none')
    })
    document.onmousemove = function (e) {
        if (isDowm) {
            var x = e.clientX;
            var target = 0;
            var left = parseInt(objX) + parseInt(x) - parseInt(mouseX);
            if(left < 0 ) {
                target = 0;
            } else if (left > max) {
                target = max;
            } else {
                target = left;
            }
            for(var i = 0; i < scroll.length; i++) {
                scroll[i].style.left = target + "px";
            }
            var left = -target/max*(sW-fW);
            content.style.marginLeft = left + "px";
            callback && callback(left)
        }
    }
    document.onmouseup = function (e) {
        $body.removeClass('select-none')
        if (isDowm) {
            isDowm = false;
        }
    }

    document.onscroll = function() {
        var page = $("#pageToolbar");
        if(page.length == 0) {
            document.onscroll = null;
            return;
        };
        var top = document.documentElement.scrollTop || document.body.scrollTop;
        // 判断是否换页了
        if(top > (page.offset().top - $(window).height()) ) {                     
            scroll.parent().removeClass("bottom")
        } else {
            scroll.parent().addClass("bottom")
        }
    }
    document.onscroll()
}
//天/时/分
function timeToMillion(times) {
//     var times;
// 　　　　　　//如果是2个参数就是时间差
//     if (endStr) {
//         var startT = new Date(startStr).getTime()
//         var endT = new Date(endStr).getTime()
//         times = (endT - startT) / 1000
// 　　　　　　// 如果是一个参数，参数值是秒数

//     } else {
//         times = startStr
//     }
    var day, hour, minute,endOutStr;
    if (times > 0) {
        // console.log(times)
        day = Math.floor(times / (60 * 60 * 24));
        hour = Math.floor(times / (60 * 60)) - (day * 24);
        minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
        // second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
        if (parseInt(day) != 0) {
            endOutStr = day + "天" + hour + "小时" + minute + "分钟"
        } else {
            if (parseInt(hour) != 0) {
                endOutStr = hour + "小时" + minute + "分钟"
            } else {
                endOutStr = minute + "分钟"
            }
        }
    }else{
        endOutStr = 0
    }
    // if (day <= 9) day = '0' + day;
    // if (hour <= 9) hour = '0' + hour;
    // if (minute <= 9) minute = '0' + minute;
    // if (second <= 9) second = '0' + second;
    return endOutStr
}